//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.action;

import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.mingsoft.base.entity.ResultData;
import net.mingsoft.base.exception.BusinessException;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.mdiy.biz.IModelBiz;
import net.mingsoft.mdiy.biz.IModelDataBiz;
import net.mingsoft.mdiy.constant.e.ModelCustomTypeEnum;
import net.mingsoft.mdiy.entity.ModelEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-自定义模块接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/mdiy/form/data"})
public class FormDataAction extends BaseAction {
    @Autowired
    private IModelDataBiz modelDataBiz;
    @Autowired
    private IModelBiz modelBiz;

    public FormDataAction() {
    }

    @ApiIgnore
    @GetMapping({"/index"})
    public String index(HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        String modelName = BasicUtil.getString("modelName");
        ModelEntity modelEntity = (ModelEntity) this.modelBiz.getOne(
                new LambdaQueryWrapper<ModelEntity>()
                        .eq(ModelEntity::getModelName, modelName)
                        .eq(ModelEntity::getModelCustomType, ModelCustomTypeEnum.FORM.getLabel())
        );
        if (modelEntity == null) {
            throw new BusinessException(this.getResString("err.not.exist", new String[]{this.getResString("model")}));
        } else {
            return "/mdiy/form/data/index";
        }
    }

    @ApiIgnore
    @GetMapping({"/form"})
    public String form(HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        String businessForm = BasicUtil.getString("businessUrl");
        return StringUtils.isNotBlank(businessForm) ? businessForm : "/mdiy/form/data/form";
    }

    @ApiOperation("提供后台查询自定义表单提交数据")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "modelId",
            value = "模型编号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "modelName",
            value = "模型名称",
            required = false,
            paramType = "query"
    )})
    @RequestMapping(
            value = {"/queryData"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public ResultData queryData(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = BasicUtil.assemblyRequestMap();
        LambdaQueryWrapper<ModelEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(ModelEntity::getId, map.get("modelId"))
                .or()
                .eq(ModelEntity::getModelName, map.get("modelName"))
                .eq(ModelEntity::getModelCustomType, ModelCustomTypeEnum.FORM);
        ModelEntity modelEntity = (ModelEntity) this.modelBiz.getOne(wrapper, false);
        if (modelEntity == null) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.id")}));
        } else if (!this.getPermissions("mdiy:formData:view", "mdiy:formData:" + modelEntity.getModelName() + ":view")) {
            return ResultData.build().error("没有权限!");
        } else {
            map.putIfAbsent("order", "desc");
            map.putIfAbsent("orderBy", "id");
            List list = this.modelDataBiz.queryDiyFormData(modelEntity.getId(), map);
            return ResultData.build().success(new EUListBean(list, (int) BasicUtil.endPage(list).getTotal()));
        }
    }

    @ApiOperation("提供后台查询自定义表单提交数据")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "modelId",
            value = "模型编号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "modelName",
            value = "模型名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "id",
            value = "主键编号",
            required = true,
            paramType = "query"
    )})
    @GetMapping({"/getData"})
    @ResponseBody
    public ResultData getData(HttpServletRequest request, HttpServletResponse response) {
        String modelId = BasicUtil.getString("modelId");
        String modelName = BasicUtil.getString("modelName");
        String id = BasicUtil.getString("id");
        LambdaQueryWrapper<ModelEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(ModelEntity::getId, modelId)
                .or()
                .eq(ModelEntity::getModelName, modelName);
        ModelEntity modelEntity = (ModelEntity) this.modelBiz.getOne(wrapper, false);
        if (modelEntity == null) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.id")}));
        } else if (StringUtils.isEmpty(id)) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("id")}));
        } else {
            Object object = this.modelDataBiz.getFormData(modelEntity.getId(), id);
            return ObjectUtil.isNotNull(object) ? ResultData.build().success(object) : ResultData.build().error();
        }
    }

    @ApiOperation("保存")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "modelName",
            value = "业务模型名称",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "modelId",
            value = "业务模型Id",
            required = false,
            paramType = "query"
    )})
    @LogAnn(
            title = "更新自定义业务数据",
            businessType = BusinessTypeEnum.INSERT
    )
    @PostMapping({"save"})
    @ResponseBody
    public ResultData save(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = BasicUtil.assemblyRequestMap();
        CaseInsensitiveMap<String, Object> caseIgnoreMap = new CaseInsensitiveMap(map);
        String modelName = BasicUtil.getString("modelName");
        String modelId = BasicUtil.getString("modelId");
        if (StringUtils.isBlank(modelName) && StringUtils.isBlank(modelId)) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.id")}));
        } else {
            LambdaQueryWrapper<ModelEntity> wrapper = new LambdaQueryWrapper();
                    wrapper.eq(StringUtils.isNotEmpty(modelName), ModelEntity::getModelName, modelName)
                    .eq(StringUtils.isNotEmpty(modelId), ModelEntity::getId, modelId)
                    .eq(ModelEntity::getModelCustomType, ModelCustomTypeEnum.FORM.getLabel());
            ModelEntity modelEntity = (ModelEntity) this.modelBiz.getOne(wrapper, true);
            if (modelEntity == null) {
                return ResultData.build().error(this.getResString("err.not.exist", new String[]{this.getResString("model.name")}));
            } else if (!this.getPermissions("mdiy:formData:save", "mdiy:formData:" + modelEntity.getModelName() + ":save")) {
                return ResultData.build().error("没有权限!");
            } else {
                return this.modelDataBiz.saveDiyFormData(modelEntity.getId(), caseIgnoreMap) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("model.id")}));
            }
        }
    }

    @ApiOperation("更新自定义业务数据")
    @ApiImplicitParam(
            name = "modelId",
            value = "模型编号",
            required = true,
            paramType = "query"
    )
    @LogAnn(
            title = "更新自定义业务数据",
            businessType = BusinessTypeEnum.UPDATE
    )
    @PostMapping({"update"})
    @ResponseBody
    public ResultData update(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = BasicUtil.assemblyRequestMap();
        CaseInsensitiveMap<String, Object> caseIgnoreMap = new CaseInsensitiveMap(map);
        String modelId = caseIgnoreMap.get("modelId").toString();
        if (StringUtils.isBlank(modelId)) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.id")}));
        } else {
            ModelEntity modelEntity = (ModelEntity) this.modelBiz.getById(modelId);
            if (!this.getPermissions("mdiy:formData:update", "mdiy:formData:" + modelEntity.getModelName() + ":update")) {
                return ResultData.build().error("没有权限!");
            } else {
                return this.modelDataBiz.updateDiyFormData(modelId, caseIgnoreMap) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("model.id")}));
            }
        }
    }

    @ApiOperation("批量删除自定义业务数据接口")
    @LogAnn(
            title = "批量删除自定义业务数据接口",
            businessType = BusinessTypeEnum.DELETE
    )
    @PostMapping({"delete"})
    @ResponseBody
    public ResultData delete(@RequestParam("modelId") String modelId, HttpServletResponse response, HttpServletRequest request) {
        String ids = BasicUtil.getString("ids");
        if (StringUtils.isBlank(ids)) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("id")}));
        } else if (StringUtils.isBlank(modelId)) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.id")}));
        } else {
            ModelEntity modelEntity = (ModelEntity) this.modelBiz.getById(modelId);
            if (!this.getPermissions("mdiy:formData:del", "mdiy:formData:" + modelEntity.getModelName() + ":del")) {
                return ResultData.build().error("没有权限!");
            } else {
                String[] _ids = ids.split(",");
                String[] var7 = _ids;
                int var8 = _ids.length;

                for (int var9 = 0; var9 < var8; ++var9) {
                    String id = var7[var9];
                    this.modelDataBiz.deleteQueryDiyFormData(id, modelId);
                }

                return ResultData.build().success();
            }
        }
    }
}
